package sdufe.lele.controller;

import org.apache.shiro.util.ThreadContext;
import org.springframework.web.bind.annotation.*;
import sdufe.lele.annotation.authToken;
import sdufe.lele.dao.ArticleMapper;
import sdufe.lele.dao.MessageMapper;
import sdufe.lele.dao.UserinfoMapper;
import sdufe.lele.model.Message;
import sdufe.lele.result.ListResult;
import sdufe.lele.result.MapResult;
import sdufe.lele.result.Result;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhnh9 on 4/19/2017.
 */

@RestController
@RequestMapping("/extra")
public class ExtraController {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private UserinfoMapper userinfoMapper;

    @Resource
    private ArticleMapper articleMapper;


    @GetMapping("/selectByPage.do")
    @authToken
    public Result selectIncomeByPage(@RequestParam(name = "page") String page) {


        ListResult listResult = new ListResult();
        String phone = (String) ThreadContext.get("user-phone");
        List<Message> mlist;

        if (phone.equals("admin")) {
            mlist = messageMapper.selectByPage(Integer.parseInt(page)-1, Integer.parseInt(page) + 100);
        } else {
            mlist = messageMapper.selectByPagAndPhone(Integer.parseInt(page)-1, Integer.parseInt(page) + 100, phone);
        }
        for (Message m : mlist) {
            if (m.getMessagelevel() == 1) {
                m.setMessageio("收入");
            } else {
                m.setMessageio("支出");
            }
            listResult.set(m);
        }

        // todo 分页插件

        return listResult;
    }

    @GetMapping("/init.do")
    @authToken
    public Result init() {
        MapResult mapResult = new MapResult();
        mapResult.put("notification", articleMapper.countAll());
        String phone = (String) ThreadContext.get("user-phone");
        if (!phone.equals("admin")) {
            mapResult.put("total_rec", messageMapper.countAllByPhone(phone));
            mapResult.put("month_rec", messageMapper.countByCurrentMonthAndPhone(phone));
        } else {
            mapResult.put("total_rec", messageMapper.countAll());
            mapResult.put("month_rec", messageMapper.countByCurrentMonth());

        }
        mapResult.put("me", messageMapper.getBalance(phone));

        return mapResult;
    }


    @GetMapping("/get_article.do")
    @authToken
    public Result get_article() {
        ListResult listResult = new ListResult();
        listResult.setAll(articleMapper.get_article());
        return listResult;
    }


}
